package com.glzn.springboot03.controller;

import com.github.pagehelper.PageInfo;
import com.glzn.springboot03.entity.RealData;
import com.glzn.springboot03.service.RealDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Lenovo
 */
@Controller
public class RealDataController {
	private static final Logger logger = LoggerFactory.getLogger(EventController.class);

	private static final String ALL_TYPE = "所有类型";

	@Autowired
	private RealDataService realDataService;

	private List<String> realDataType = new ArrayList<>();

	@RequestMapping("/getrealdata")
	@ResponseBody
	public String getRealData() throws UnsupportedEncodingException {
		return realDataService.listRealData();
	}

	@GetMapping(value = "/getrealdata/getprocess")
	@ResponseBody
	public int getProcess(HttpSession session){
		return realDataService.getProcess(session.getId());
	}

	@RequestMapping("/realdatas")
	public String realData(ModelMap modelMap,
	                       HttpSession session,
	                       @RequestParam(required = false) String realDataType,
	                       @RequestParam(required = false, defaultValue = "1") int pageNum,
	                       @RequestParam(required = false, defaultValue = "10") int pageSize) {
		Integer deviceId = (Integer) session.getAttribute("device_id");
		if (null == realDataType || "".equals(realDataType)) {
			realDataType = ALL_TYPE;
		}
		//所有类型处理
		List<RealData> realDataList = realDataService.findAll(deviceId);
		this.realDataType.clear();
		this.realDataType.add(ALL_TYPE);
		for (RealData realData : realDataList) {
			int i = 0;
			for (i = 0; i < this.realDataType.size(); i++) {
				if (this.realDataType.get(i).equals(realData.getType())){
					break;
				}
			}
			if (i == this.realDataType.size()){
				this.realDataType.add(realData.getType());
			}
		}
		//页面数据
		PageInfo<RealData> pageInfo = null;
		if (realDataType.equals(ALL_TYPE)) {
			pageInfo = realDataService.findAll(pageNum, pageSize, deviceId);
		} else {
			pageInfo = realDataService.findByType(realDataType, pageNum, pageSize, deviceId);
		}
		long totalNum = pageInfo.getTotal() / pageSize + 1;

		modelMap.put("realDataType", this.realDataType);
		modelMap.put("pageInfo", pageInfo);
		modelMap.put("totalNum", totalNum);

		return "realdata";
	}

	@GetMapping("/wx/realdatas")
	@ResponseBody
	public List<RealData> wxPara(HttpSession session, String type){
		Integer deviceId = (Integer) session.getAttribute("device_id");
		if(ALL_TYPE.equals(type) || "".equals(type)){
			return realDataService.findAll(deviceId);
		}else {
			return realDataService.findByType(type, deviceId);
		}
	}
}
